package org.jeecg.modules.dictionaries.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.dictionaries.entity.DmMeterReading;
import org.jeecg.modules.dictionaries.entity.TdEnergyTypes;
import org.jeecg.modules.dictionaries.service.IDmMeterReadingService;
import org.jeecg.modules.dictionaries.service.ITdEnergyTypesService;
import org.jeecg.modules.equipment.gasmeter.entity.DmGasmeter;
import org.jeecg.modules.equipment.gasmeter.service.IDmGasmeterService;
import org.jeecg.modules.equipment.steammeter.entity.DmSteammeter;
import org.jeecg.modules.equipment.steammeter.service.IDmSteammeterService;
import org.jeecg.modules.equipment.watermeter.entity.DmWatermeter;
import org.jeecg.modules.equipment.watermeter.service.IDmWatermeterService;
import org.jeecg.modules.equipment.watthourmeter.entity.DmWatthourmeter;
import org.jeecg.modules.equipment.watthourmeter.service.IDmWatthourmeterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 手动抄表
 * @Author: jeecg-boot
 * @Date: 2021-12-06
 * @Version: V1.0
 */
@Api(tags = "手动抄表")
@RestController
@RequestMapping("/electricity/dmMeterReading")
@Slf4j
public class DmMeterReadingController extends JeecgController<DmMeterReading, IDmMeterReadingService> {
    @Autowired
    private IDmMeterReadingService dmMeterReadingService;
    @Autowired
    private ITdEnergyTypesService tdEnergyTypesService;
    @Autowired
    private IDmGasmeterService dmGasmeterService;//燃气表管理
    @Autowired
    private IDmWatthourmeterService dmWatthourmeterService;//电表管理啊

    @Autowired
    private IDmWatermeterService dmWatermeterService;//水表管理

    @Autowired
    private IDmSteammeterService dmSteammeterService;//蒸汽表管理

    /**
     * 分页列表查询
     *
     * @param dmMeterReading
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "手动抄表-分页列表查询")
    @ApiOperation(value = "手动抄表-分页列表查询", notes = "手动抄表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(DmMeterReading dmMeterReading,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        String tenantId = req.getHeader("tenant-id");
        //获取到的租户id为0的情况下是admin管理员是可以看到全部的
        if (!tenantId.equals("0")) {
            dmMeterReading.setTenantId(Integer.parseInt(tenantId));
        }
        QueryWrapper<DmMeterReading> queryWrapper = QueryGenerator.initQueryWrapper(dmMeterReading, req.getParameterMap());
        Page<DmMeterReading> page = new Page<DmMeterReading>(pageNo, pageSize);
        IPage<DmMeterReading> pageList = dmMeterReadingService.page(page, queryWrapper);
        List<DmMeterReading> records = pageList.getRecords();
        for (DmMeterReading d : records) {
            String equipmentId = d.getEquipmentId();
            //根据能源id查询能源名称
            TdEnergyTypes byId = tdEnergyTypesService.getById(equipmentId);
            d.setEquipmentIdName(byId.getEnergyName());
            //根据能源code查询对应的设备表
            String code = byId.getTheStandby1();
            String managementId = d.getManagementId();
            switch (code) {
                case "RQ":
                    DmGasmeter byId1 = dmGasmeterService.getById(managementId);
                    d.setManagementIdName(byId1.getGmName());
                    break;
                case "ZQ":
                    DmSteammeter byId2 = dmSteammeterService.getById(managementId);
                    d.setManagementIdName(byId2.getUmName());
                    break;
                case "S":
                    DmWatermeter byId3 = dmWatermeterService.getById(managementId);
                    d.setManagementIdName(byId3.getWmName());
                    break;
                case "DB":
                    DmWatthourmeter byId4 = dmWatthourmeterService.getById(managementId);
                    d.setManagementIdName(byId4.getWhmName());
                    break;
            }
        }
        return Result.OK(pageList);
    }


    /**
     * 查出来上一天抄表数据
     *
     * @return
     */
    @AutoLog(value = "手动抄表-查询")
    @ApiOperation(value = "手动抄表-查询", notes = "手动抄表-查询")
    @GetMapping(value = "/yesterdaySele")
    public Result<?> yesterdaySele(@RequestParam(name = "managementId") String managementId, HttpServletRequest req) {
        DmMeterReading dmMeterReading = new DmMeterReading();
        dmMeterReading.setManagementId(managementId);
        dmMeterReading.setTenantId(Integer.parseInt(req.getHeader("tenant-id")));
        DmMeterReading dmMeterReading1 = dmMeterReadingService.yesterdaySele(dmMeterReading);
        return Result.OK(dmMeterReading1);
    }

    /**
     * 添加
     *
     * @param dmMeterReading
     * @return
     */
    @AutoLog(value = "手动抄表-添加")
    @ApiOperation(value = "手动抄表-添加", notes = "手动抄表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody DmMeterReading dmMeterReading, HttpServletRequest req) {
        dmMeterReading.setTenantId(Integer.parseInt(req.getHeader("tenant-id")));
        dmMeterReadingService.save(dmMeterReading);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param dmMeterReading
     * @return
     */
    @AutoLog(value = "手动抄表-编辑")
    @ApiOperation(value = "手动抄表-编辑", notes = "手动抄表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody DmMeterReading dmMeterReading) {
        dmMeterReadingService.updateById(dmMeterReading);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "手动抄表-通过id删除")
    @ApiOperation(value = "手动抄表-通过id删除", notes = "手动抄表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        dmMeterReadingService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "手动抄表-批量删除")
    @ApiOperation(value = "手动抄表-批量删除", notes = "手动抄表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.dmMeterReadingService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "手动抄表-通过id查询")
    @ApiOperation(value = "手动抄表-通过id查询", notes = "手动抄表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        DmMeterReading dmMeterReading = dmMeterReadingService.getById(id);
        if (dmMeterReading == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dmMeterReading);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param dmMeterReading
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DmMeterReading dmMeterReading) {
        return super.exportXls(request, dmMeterReading, DmMeterReading.class, "手动抄表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DmMeterReading.class);
    }

}
